package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Api(tags = "分类信息相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类管理,分页查询
     *
     * @return
     */
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "page",value = "页码",readOnly = true),
                    @ApiImplicitParam(name = "pageSize",value = "当前页展示条数",readOnly = true)
            }
    )
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize) {
        log.info("接受到页码:{}接收到展示条数:{}", page, pageSize);
        //分页构造
        Page<Category> pages = new Page<>(page, pageSize);
        //添加排序条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        //查询方法
        Page<Category> categoryPage = categoryService.page(pages, queryWrapper);
        log.info("分类信息已查询，为:{}", categoryPage.getRecords());
        return R.success(categoryPage);
    }

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "新增分类")



    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("新的分类信息为category:{}", category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 根据id更新数据
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "更新分类数据")



    @PutMapping
    public R<String> UpdateById(@RequestBody Category category) {
        log.info("接受页面传输的分类更新信息:{}", category);
        categoryService.updateById(category);
        return R.success("更新成功");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除分类数据")

    @ApiImplicitParam(name = "id", value = "分类id", readOnly = true)


    @DeleteMapping
    public R<String> dalete(Long id) {
        log.info("接受页面传输的id:{}", id);
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 根据条件查询分类数据
     *
     * @return
     */
    @ApiOperation(value = "根据条件查询分类")
    @GetMapping("/list")
    public R<List<Category>> list(Category type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type.getType() != null, Category::getType, type.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
